﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using IDAL;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
   public  class CustomerDal:ICustomerDal
    {
        public static string Conns = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;

        public List<StaffModel> CustShow()
        {
            using (SqlConnection conn = new SqlConnection(Conns))
            {
                conn.Open();
                string sql = "select UserID,UserTel,UserPwd,UserIntegral,(case when UserState=1 then '解冻' when UserState=0 then '冻结' end)  as UserState,CONVERT(varchar(30),UserRegTime,23) as UserRegTime from UserTable where JurID=2";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader read = cmd.ExecuteReader();
                List<StaffModel> list = new List<StaffModel>();
                while (read.Read())
                {
                    StaffModel mo = new StaffModel();
                    mo.UserID = Convert.ToInt32(read["UserID"]);
                    mo.UserTel = read["UserTel"].ToString();
                    mo.UserPwd = read["UserPwd"].ToString();
                    mo.UserIntegral = Convert.ToInt32(read["UserIntegral"]);
                    mo.UserState = read["UserState"].ToString();
                    mo.UserRegTime = read["UserRegTime"].ToString();
                    list.Add(mo);

                }
                conn.Close();
                return list;
            }
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int CustUpt(int id)
        {
            using (SqlConnection conn = new SqlConnection(Conns))
            {
                conn.Open();
                string sql = string.Format("update UserTable set UserState=1 where UserID='{0}'", id);
                SqlCommand cmd = new SqlCommand(sql, conn);
                int i = cmd.ExecuteNonQuery();
                conn.Close();
                return i;
            }
        }



        /// <summary>
        /// 停用状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int CustDel(int id)
        {
            using (SqlConnection conn = new SqlConnection(Conns))
            {
                conn.Open();
                string sql = string.Format("update UserTable set UserState=0 where UserID='{0}'", id);
                SqlCommand cmd = new SqlCommand(sql, conn);
                int i = cmd.ExecuteNonQuery();
                conn.Close();
                return i;
            }
        }
    }
}
